package com.huoxingy.statics.controllers;

import com.huoxingy.statics.services.MemberCountStaticsService;
import com.huoxingy.statics.services.TotalMemberConsumptionService;
import com.huoxingy.statics.vo.TotalMemberConsumptionsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by hanxi on 2018/1/6.
 */
@RestController
@RequestMapping(path = "/v1")
public class MemberController {

    @Autowired
    private MemberCountStaticsService memberCountStaticsService;


    @Autowired
    TotalMemberConsumptionService totalMemberConsumptionService;

    /**
     * 获取过去7天会员信息
     *
     * @param storeId
     * @return
     */
    @GetMapping(path = "/stores/{storeId}/daily-member-statics")
    public ResponseEntity<?> getDailyMemberStatics(@PathVariable String storeId) {

        List<Map<String, Object>> statics = memberCountStaticsService.findOneByStoreIdAndBizDateAfter(storeId);
        if (null == statics) {
            return ResponseEntity.notFound().build();
        } else {
            return ResponseEntity.ok(statics);
        }
    }

    /**
     * 获取会员消费汇总
     *
     * @param storeId
     * @return
     */
    @GetMapping(path = "/stores/{storeId}/total_member_statics")
    public ResponseEntity<?> getTotalMemberStatics(@PathVariable String storeId,
                                                   @PageableDefault(value = 10, sort = {"amount"}, direction = Sort.Direction.DESC) Pageable pageable) {

        TotalMemberConsumptionsVo consumptionsVo = totalMemberConsumptionService.getTotalMemberConsumption(storeId, pageable);
        if (null == consumptionsVo) {
            return ResponseEntity.notFound().build();
        } else {
            return ResponseEntity.ok(consumptionsVo);
        }
    }

    /**
     * 获取会员流失信息
     *
     * @param storeId
     * @return
     */
    @GetMapping(path = "/stores/{storeId}/lost_member_statics")
    public ResponseEntity<?> getLostMemberStatics(@PathVariable String storeId,
                                                  @PageableDefault(value = 10, sort = {"amount"}, direction = Sort.Direction.DESC) Pageable pageable) {

        TotalMemberConsumptionsVo consumptionsVo = totalMemberConsumptionService.getLostMemberConsumption(storeId, pageable);
        if (null == consumptionsVo) {
            return ResponseEntity.notFound().build();
        } else {
            return ResponseEntity.ok(consumptionsVo);
        }
    }


    /**
     * 获取会员流失信息
     *
     * @param storeId
     * @return
     */
    @GetMapping(path = "/stores/{storeId}/lost_memberIds_all")
    public ResponseEntity<?> getLostMemberIdsAll(@PathVariable String storeId) {
        List<String> memberIds = new ArrayList<>();
        List<Map<String, Object>> maps = totalMemberConsumptionService.getLostMemberIds(storeId);
        for (Map<String, Object> map : maps) {
            memberIds.add(map.get("memberId").toString());
        }
        return ResponseEntity.ok(memberIds);
    }

}
